<?php

namespace App\Http\Filters;

use Illuminate\Database\Eloquent\Builder;

class MembersFilter extends AbstractFilter
{

    public const CENTERS_ID = 'centers_id';
    public const DIAGNOSIS = 'diagnosis';

    /**
     * @return array
     */
    protected function getCallbacks(): array
    {
        return [
            self::DIAGNOSIS => [$this, self::DIAGNOSIS],
            self::CENTERS_ID => [$this, self::CENTERS_ID],
        ];
    }




    public function centers_id(Builder $builder, $value)
    {
        $this->filterLogic($builder, $value, self::CENTERS_ID);
    }

    public function diagnosis(Builder $builder, $value)
    {
       $this->filterLogic($builder, $value, self::DIAGNOSIS);
    }

    protected function filterLogic(Builder $builder, $value, $column)
    {



        $builder->where(function ($query) use ($value, $column, ){
            foreach ($value as $key => $val) {
            if ($key > 0) {
                $query->orWhere($column, trim($val));
            } else {
                $query->where($column, trim($val));
            }

        }
        });


    }
}
